package controller.api;

import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import component.HttpClient;
import dto.OrderNotifyDto;
import dto.ResponseDto;
import dto.StatusDto;
import handler.MerchantWebSocketHandler;
import pojo.Order;
import service.AdvertisementService;
import service.MerchantService;
import service.OrderService;
import util.JsonUtil;

@Controller
public class NotifyController {
	
	private Logger logger = LogManager.getLogger(NotifyController.class);
	
	@Resource(name="merchantWebSocketHandler")
	private MerchantWebSocketHandler handler;
	
	 @Resource
	 Environment env;
	 @Resource(name="jerseyClient")
	 HttpClient httpClient;

	 @Resource(name = "merchantServiceImpl")
	 private MerchantService merchantService;
	@Resource(name="orderServiceImpl")
	private OrderService orderService;
	
	@Resource
	private AdvertisementService advertisementService;
	
	
	//只有白名单通知 有时间换切面
	@RequestMapping(value="/api/orders",method=RequestMethod.POST)
	@ResponseBody
	public Object notify(@RequestBody OrderNotifyDto notifyDto) {
		Order order = orderService.findByprocessNumber(notifyDto.getOrderNumber());
		try {
			String status =httpClient.get(env.getProperty("get.status.url")+order.getMerchant().getName());
			 StatusDto statusDto = (StatusDto) JsonUtil.toJsonObject(status, StatusDto.class);
				if (StatusDto.WHITE_LIST.equals(statusDto.getStatus())) {
					ResponseDto responseDto = new ResponseDto();
					responseDto.setCode(2);
					responseDto.setMessage("new order "+notifyDto.getOrderNumber());
					responseDto.setData(notifyDto.getOrderNumber());
					handler.sendTo(notifyDto.getMerchantId(), JsonUtil.toJsonString(responseDto));
				}
				ResponseDto res = new ResponseDto(1,"success",null);
				return res;
		}catch (Exception e) {
			logger.error(e);
			ResponseDto res = new ResponseDto(1,"success",null);
			return res;
		}
    	
       
	}
	
	@RequestMapping(value="/api/merchant/status",method=RequestMethod.POST)
	@ResponseBody
	public Object notifyMerchantStatus(@RequestBody StatusDto statusDto) {
		ResponseDto responseDto = new ResponseDto();
		responseDto.setCode(1);
		responseDto.setMessage("you have add to "+statusDto.getStatus());
		responseDto.setData(statusDto.getStatus());
		System.out.println(statusDto.getMerchantName()+statusDto.getStatus());
		handler.sendToByName(statusDto.getMerchantName(), JsonUtil.toJsonString(responseDto));
		ResponseDto res = new ResponseDto(1,"success",null);
		return res;
	}
	
//	@RequestMapping(value="/api/advertise/status",method=RequestMethod.POST)
//	@ResponseBody
//	public Object notifyAdvertiseStatus(@RequestBody AdSpaceToMDto adSpaceTo) {
//		ResponseDto responseDto = new ResponseDto();
//		responseDto.setCode(1);
//		responseDto.setMessage("you advtise have add to "+adSpaceTo.getStatus());
//		responseDto.setData(adSpaceTo.getStatus());
////		Advertisement advertisement = advertisementService.findAll(mId);
////		handler.sendToByName(statusDto.getMerchantName(), JsonUtil.toJsonString(responseDto));
//		ResponseDto res = new ResponseDto(1,"success",null);
//		return res;
//	}
	
	

}
